package com.example.demo.controller;

import com.example.demo.service.AnnoService;
import com.example.demo.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/auth")
public class JwtAuthController {


    @RequestMapping("/login")
    public ResponseEntity<?> loginAndGenerateToken(@RequestBody String username, @RequestBody String pwd) {
        //@RequestBody String username, @RequestBody String pwd

        UserDetails user1 = User.
                withUsername(username)
                .password(new BCryptPasswordEncoder().encode(pwd)).roles(username).build();
        JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
        String token = jwtTokenUtil.generateToken(user1);

        return ResponseEntity.ok(token);
        //eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTcyMjA5MzY1NSwiZXhwIjoxNzIyMTgwMDU1fQ.aGTQ3OaQOW9-QEDEzeZhTrpvGOcuZyLav7ZqeuGVPpA
    }

}
